<?php
class MenuAction extends CommonAction {
	public function _filter(&$map)
	{
		//$map['status'] = 1;
		$map['pid']	=	0;
		if($_GET['pid']!=''){
			$map['pid']=$_GET['pid'];
		}
		$_SESSION['currentMenuId']	=	$map['pid'];
		//获取上级节点
		$menu  = M("Menu");
        if(isset($map['pid'])) {
            if($menu->getById($map['pid'])) {
                $this->assign('level',$menu->level+1);
                $this->assign('MenuName',$menu->name);
            }else {
                $this->assign('level',1);
            }
        }
	}

	// 获取配置类型
	public function _before_add() {
		$menu	=	M("Menu");
		$pid  = $_REQUEST['pid'] ? $_REQUEST['pid'] : 0;
		//更改由url获取pid
		$menu->getById($pid);
		$role=M('Role');
		$role_names=$role->field('id,name')->select();
		$this->assign('role_names',$role_names);
		$this->assign('pid',$menu->id);
		$this->assign('level',$menu->level+1);

		//获取树状菜单
		Vendor('TreeUnit.Tree');
		$menu	=	M("Menu");
		$menulist = $menu->where("status = 1")->findAll();
		//dump($menulist);
		$menulist = array_reindex($menulist);
		$str="<option value=\$id \$selected >\$spacer\$name</option>";
		$tree=new Tree($menulist);
     		$menu_tree = $tree->get_tree(0,$str,$pid);
     		$this->assign('menu_tree', $menu_tree);
	}


	/*
	 * 首页列表
	 */
	public function index() {
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$this->_list ( $model, $map, 'sort' );
		}
		$this->display ();
		return;
	}



    public function _before_edit() {
		Vendor('TreeUnit.Tree');
		$name=$this->getActionName();
		$model = M ( $name );
		$id = $_REQUEST [$model->getPk ()];
		$vo = $model->getById ($id);

		//获取菜单数据并从新索引
		$menu	=	M("Menu");
		$menulist = $menu->where("status = 1")->findAll();
		$menulist = array_reindex($menulist);

		//获取树状菜单
		$str="<option value=\$id \$selected >\$spacer\$name</option>";
		$tree=new Tree($menulist);
		$menu_tree = $tree->get_tree(0,$str,$vo['pid']);

		$ids=$_REQUEST['id'];
		$role=M('RbacRole');
		$role_names=$role->field('id,name')->select();
		$keys=$menu->where("id=$ids")->field('role_ids')->select();
		$keyid=$keys[0]['role_ids'];
		if($keyid!="")
		{
			$this->assign('keyid',$keyid);
		}else{
		$keyid='0';
		$this->assign('keyid',$keyid);
		}
		$this->assign('role_names',$role_names);
     	$this->assign("menu_tree", $menu_tree);
	}


    /*
     * 修复菜单数据，修复isfolder数据
     */
    public function repair()
    {
    	$model = M('Menu');
    	$list = $model->where('status = 1')->select();
    	//循环遍历数据
    	foreach ($list as $r)
    	{
    		//获取数据
    		$map['pid'] = $r['id'];
    		$map['status'] = 1;
    		$l = $model->where($map)->select();
    		if($l) $model->execute("update qidi_menu set isfolder=1 where id='".$r['id']."'");
    		else $model->execute("update qidi_menu set isfolder=0 where id='".$r['id']."'");
    	}

    	if ($list!==false) {
			$this->success ('更新成功!');
		} else {
			$this->error ('没有需要更新的数据!');
		}
    }

    /*
     * 导入栏目数据到菜单
     * 顶级栏目导入到iscategory=1的菜单下
     */
    public function importcategory($pid = 0)
    {
		//获取树状菜单
		Vendor('TreeUnit.Tree');
		$menu	=	M("Menu");
		$map['status'] = 1;
		$map['iscategory'] = 1;
		$menulist = $menu->where($map)->limit(1)->findAll();
		$menulist[0]['pid'] = 0;
		$str="<option value=\$id \$selected >\$spacer\$name</option>";
		$tree=new Tree($menulist);
     	$menu_tree = $tree->get_tree(0,$str,0);
     	$this->assign('menu_tree', $menu_tree);

    	//循环栏目
    	$this->display();
    }

    /*
     * @param pid,栏目父id
     * @param mpid,菜单父id
     */
    public function doimport($pid = 0, $mpid = 0, $level = 0)
    {
    	//鉴别menu.pid
    	$mpid = $_REQUEST['mpid'] ? $_REQUEST['mpid'] : $mpid;
    	unset($_REQUEST['mpid']);

    	//获取所有模块数据
    	$model = M('Module');
    	$MODULES = $model->field('id,module')->select();
    	$MODULES = array_reindex($MODULES);

    	$model = M('Menu');
    	$category = M('Category');
    	//获取栏目数据
    	$map['pid'] = $pid;
    	$map['status'] = 1;
    	$list  = $category->field('id,name,brief_name,module_id,cid')->where($map)->order('id desc')->select();

    	//遍历栏目
    	foreach($list as $r)
    	{
    		unset($map['status']);
    		$map['pid'] = $mpid;
			$map['action'] = array('like','%/'.$r['id'].'/%');
    		//检查重复项
    		$have = $model->field('id')->where($map)->select();

    		if(count($have) > 0)
    		{
    			$mapson['pid'] = $r['id'];
    			$haveson = $category->field('id')->where($mapson)->select();
    			//字段
    			$field['name'] = 'name';
    			$field['brief_name'] = 'brief_name';
    			$field['pid'] = 'pid';
    			$field['isfolder'] = 'isfolder';
    			$field['action'] = 'action';
    			$field['status'] = 'status';
    			//值
    			$data['name'] = $r['name'];
    			$data['brief_name'] = $r['brief_name'];
    			$data['pid'] = $mpid;
    			$data['isfolder'] = count($haveson) > 0 ? 1 :0;
    			$data['action'] = $MODULES[$r['module_id']]['module'].'/index/category_id/'.$r['id'].'/';
    			$data['status'] = 1;

    			$condition['id'] = $have[0]['id'];
    			$model->setField($field,$data,$condition);
    			$nmpid = $have[0]['id'];

    		}
    		else
    		{
    			//不存在就插入
    			$data['name'] = $r['name'];
    			$data['brief_name'] = $r['brief_name'];
    			$data['pid'] = $mpid;
    			$data['isfolder'] = $r['cid'];
    			$data['action'] = $MODULES[$r['module_id']]['module'].'/index/category_id/'.$r['id'].'/';
    			$data['status'] = 1;
    			$model->add($data);
    			$nmpid = $model->getLastInsID();
    		}
    		//如果有子栏目需要遍历
    		unset($map);
    		$map['status'] = 1;
    		$map['pid'] = $r['id'];
    		$have = $category->field('id')->where($map)->select();
    		if($have>0)
    		{
    			$level += 1;
    			$this->doimport($r['id'],$nmpid, $level);
    		}
    		else
    		{
    			;
    		}
    	}
    	//echo 'ok';
    	//$this->success ('导入成功!');

    }

}
?>